package com.gougou.search.service;

import com.gougou.search.entity.Item;
import com.gougou.search.entity.SearchResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ItemSearchService
 * @Description
 * @Author c
 * @Date 2018/9/14 14:35
 * @Vcrsion 1.0
 **/
public class ItemSearchService {

    private HttpSolrServer httpSolrServer;

    public ItemSearchService(HttpSolrServer httpSolrServer){
        this.httpSolrServer = httpSolrServer;
    }

    /**
     * solr查询
     * @param keyWords
     * @param page
     * @param rows
     * @return
     * @throws SolrServerException
     */
    public SearchResult search(String keyWords, Integer page, Integer rows) throws SolrServerException {
        SolrQuery solrQuery = new SolrQuery(); //构造搜索条件
        solrQuery.setQuery("title:"+keyWords+" AND status:1"); //搜索关键词
        // 设置分页 start=0就是从0开始，，rows=5当前返回5条记录，第二页就是变化start这个值为5就可以了。
        solrQuery.setStart((Math.max(page, 1) - 1) * rows);
        solrQuery.setRows(rows);

        //是否需要高亮
        boolean isHighlighting = !StringUtils.equals("*", keyWords) && StringUtils.isNotEmpty(keyWords);

        if (isHighlighting) {
            // 设置高亮
            solrQuery.setHighlight(true); // 开启高亮组件
            solrQuery.addHighlightField("title");// 高亮字段
            solrQuery.setHighlightSimplePre("<em>");// 标记，高亮关键字前缀
            solrQuery.setHighlightSimplePost("</em>");// 后缀
        }

        // 执行查询
        QueryResponse queryResponse = httpSolrServer.query(solrQuery);
        List<Item> items = queryResponse.getBeans(Item.class);
        if (isHighlighting) {
            // 将高亮的标题数据写回到数据对象中
            Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
            for (Map.Entry<String, Map<String, List<String>>> highlighting : map.entrySet()) {
                for (Item item : items) {
                    if (!highlighting.getKey().equals(item.getId().toString())) {
                        continue;
                    }
                    item.setTitle(StringUtils.join(highlighting.getValue().get("title"), ""));
                    break;
                }
            }
        }
        return new SearchResult(queryResponse.getResults().getNumFound(),items);
    }

    /**
     * 添加/更新数据到solr
     * @param item
     * @throws IOException
     * @throws SolrServerException
     */
    public void add(Item item) throws IOException, SolrServerException {
        httpSolrServer.addBean(item);
        httpSolrServer.commit();
    }

    /**
     * 删除solr上的数据
     * @param itemId
     * @throws IOException
     * @throws SolrServerException
     */
    public void delete(String itemId) throws IOException, SolrServerException {
        httpSolrServer.deleteById(itemId);
        httpSolrServer.commit();
    }

    /**
     * 删除solr上的所有数据
     * @throws IOException
     * @throws SolrServerException
     */
    public void deleteAll() throws IOException, SolrServerException {
        httpSolrServer.deleteByQuery("*:*");
        httpSolrServer.commit();
    }
}
